﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.Helpers;
using Fayshine.VA.Models;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.ViewModels;
using Visifire.Charts;
using LocalBusyIndicator = Fayshine.VA.Controls.BusyIndicator;

namespace Fayshine.VA.Views.SearchStatistics.Month
{
    public partial class EveryMonthStatistics : Page
    {
        private StatisticsReportViewModel m_ViewModel;

        public EveryMonthStatistics()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(EveryMonthStatistics_Loaded);
        }

        private void EveryMonthStatistics_Loaded(object sender, RoutedEventArgs e)
        {
            LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
            indicator.IsBusy = true;

            cmbYear.ItemsSource = FayshineManager.Instance.GetYearSource();
            cmbYear2.ItemsSource = FayshineManager.Instance.GetYearSource();
            cmbYear.SelectedValue = DateTime.Now.Year;
            cmbYear2.SelectedValue = DateTime.Now.Year;

            cmbMonth.ItemsSource = FayshineManager.Instance.GetMonthSource();
            cmbMonth2.ItemsSource = FayshineManager.Instance.GetMonthSource();
            cmbMonth2.SelectedValue = DateTime.Now.Month;
            cmbMonth.SelectedValue = DateTime.Now.AddMonths(-1).Month;

            m_ViewModel = new StatisticsReportViewModel();
             m_ViewModel.ResetHisotryChartEvent += new ResetHistoryChartEventHandler(m_ViewModel_ResetHisotryChartEvent);
            m_ViewModel.AppendHistoryPassengerFlow += new AppendHistoryPassengerFlowEventHandler(m_ViewModel_AppendHistoryPassengerFlow);
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllAreas();
        }

        void m_ViewModel_AppendHistoryPassengerFlow(string title, bool isIn, List<HistoryPassengerFlow> firstHistoryPassengerFlow, List<HistoryPassengerFlow> secondHistoryPassengerFlow)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                Axis xAxis = new Axis();

                vcChart1.AxesX.Add(xAxis);

                Title vcChart1Title = new Visifire.Charts.Title();
                vcChart1Title.Text = title;
                vcChart1.Titles.Add(vcChart1Title);
                RenderAs render = RenderAs.Column;

                DataSeries series = ChartHelper.GetDataSeries(render, "进客流", ChartValueTypes.Auto);
                series.DataMappings.Add(ChartHelper.GetDataMapping("AxisXLabel", "DisplayTitle"));
                series.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "InCount"));
                series.DataSource = firstHistoryPassengerFlow;
                vcChart1.Series.Add(series);

                DataSeries series2 = ChartHelper.GetDataSeries(render, "出客流", ChartValueTypes.Auto);
                series2.DataMappings.Add(ChartHelper.GetDataMapping("AxisXLabel", "DisplayTitle"));
                series2.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "OutCount"));
                series2.DataSource = firstHistoryPassengerFlow;
                vcChart1.Series.Add(series2);

                this.dataGrid.ItemsSource = firstHistoryPassengerFlow;

            });
        }

        private void m_ViewModel_ResetHisotryChartEvent()
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                if (vcChart1 != null)
                {
                    vcChart1.AxesX.Clear();
                    vcChart1.TrendLines.Clear();
                    vcChart1.Series.Clear();
                    vcChart1.Titles.Clear();
                }
            });
        }

        private void cmbAreas_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                List<area> selectedArea = new List<area>();
                var selectAreaExt = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
                selectAreaExt.ForEach(b =>
                {
                    area temp = b.Tag as area;
                    selectedArea.Add(temp);
                });

                if (selectedArea.Count > 0)
                {
                    cbItemDisplay.Content = selectedArea.Count > 1 ? selectedArea[0].Name + "..." : selectedArea[0].Name;

                    if (selectedArea.Count > 1)
                    {
                        this.cmbPassenger.IsEnabled = false;
                    }
                    else
                    {
                        this.cmbPassenger.IsEnabled = true;
                    }

                }
                else
                {
                    cbItemDisplay.Content = string.Empty;
                }
                cmbAreas.SelectedItem = null;
                cmbAreas.SelectedItem = cbItemDisplay;

                m_ViewModel.SelectAreasChanged(selectedArea);
                cmbPassenger_DropDownClosed(null, null);
            }
        }

        private void cmbPassenger_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                var selectMonitorAreaExts = m_ViewModel.MonitorAreaGateExts.Where(a => a.IsSelected).ToList();

                if (selectMonitorAreaExts.Count > 0)
                {
                    cbItemDisplay2.Content = selectMonitorAreaExts.Count > 1 ? selectMonitorAreaExts[0].Name + "..." : selectMonitorAreaExts[0].Name;

                }
                else
                {
                    cbItemDisplay2.Content = string.Empty;
                }
                cmbPassenger.SelectedItem = null;
                cmbPassenger.SelectedItem = cbItemDisplay2;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string msg = string.Empty;
            
            m_ViewModel.UpdateCheckedAreas();

            if (!FayshineManager.Instance.ValidateAreaGateInputs(m_ViewModel.CheckedAreas,
                m_ViewModel.CheckedGates, ref msg))
            {
                MessageBox.Show(msg);
            }
            else
            {
                int year1 = (int)cmbYear.SelectedValue;
                int month1 = (int)cmbMonth.SelectedValue;
                int year2 = (int)cmbYear2.SelectedValue;
                int month2 = (int)cmbMonth2.SelectedValue;
                DateTime startDate = new DateTime(year1, month1, 1);
                DateTime endDate = new DateTime(year2, month2, 1);
                if (startDate < endDate)
                {
                    m_ViewModel.RefreshEveryMonthStatisticsReport(true, startDate, endDate);
                }
                else
                {
                    MessageBox.Show("起始日期不能大于结束日期");
                }
            }
        }

        private string GetChartSubTitle()
        {
            string title1 = string.Empty;

            var selectAreaExts = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
            if (selectAreaExts.Count == 1)
            {
                title1 = selectAreaExts[0].Name;
            }
            else if (selectAreaExts.Count > 1)
            {
                title1 = "各店铺";
            }

            return title1;
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fsd = new SaveFileDialog();
            fsd.Filter = "PDF (*.pdf)|*.pdf|*PNG (*.png)|*.png";

            if (fsd.ShowDialog() == true)
            {
                LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
                indicator.IsBusy = true;
                this.Dispatcher.BeginInvoke(delegate()
                {
                    FayshineManager.Instance.ExportToDisk(fsd, spContent);
                });
            }
        }

    }
}
